//author:杨东升，刘江岳
Page({
  goToChinese: function() {
    wx.navigateTo({
      url: '/pages/chinese/chinese'
    });
  },
  data: {
    questions: [],       // 题目数据
    currentQuestion: 0,   // 当前题目索引
    count: 0,      // 已回答的题目数
    showQuestion: false,   // 控制题目是否展示的标志
    response: ""     ,     // 存储答题结果的字符串
    showStartButton: true, // 控制开始练习按钮的显示与隐藏
    used:[100]//题目标记位，该数组大小限制了允许加入题库的提数，暂设为100
  },

  onLoad: function() {
    // 在页面加载时设置题目数据
    const sampleQuestions = [
      //Group1
      { question: '春眠不觉晓，_________。', answer: '处处闻啼鸟' },
      { question: '谁言寸草心，_________。', answer: '报得三春晖' },
      { question: '红豆生南国，_________?', answer: '春来发几枝' },
      { question: '好雨知时节，_________。', answer: '当春乃发生' },
      { question: '野火烧不尽，_________。', answer: '春风吹又生' },
      { question: '春种一粒粟，_________。', answer: '秋收万颗子' },
      { question: '国破山河在，_________。', answer: '城春草木深' },
      { question: '落红不是无情物，_____________。', answer: '化作春泥更护花' },
      { question: '不知细叶谁裁出，_____________。', answer: '二月春风似剪刀' },
      { question: '春色满园关不住，_____________。', answer: '一枝红杏出墙来' },
      //Group2
      { question: '力尽不知热，_________。', answer: '但惜夏日长' },
      { question: '深居俯夹城，_________。', answer: '春去夏犹清' },
      { question: '首夏犹清和，_________。', answer: '芳草亦未歇' },
      { question: '仲夏苦夜短，_________。', answer: '开轩纳微凉' },
      { question: '农夫方夏耘，_________。', answer: '安坐吾敢食' },
      { question: '人皆苦炎热，_________。', answer: '我爱夏日长' },
      { question: '残云收夏暑，_________。', answer: '新雨带秋岚' },
      { question: '连雨不知春去，_____________。', answer: '一晴方觉夏深' },
      { question: '清江一曲抱村流，_____________。', answer: '长夏江村事亭幽' },
      { question: '芳菲歇去何须恨，_____________。', answer: '夏木阳阴正可人' },
      //Group3
      { question: '秋风萧瑟，_________。', answer: '洪波涌起' },
      { question: '解落三秋叶，_________。', answer: '能开二月花' },
      { question: '_________，秋收万颗子。', answer: '春种一粒粟' },
      { question: '常恐秋节至，_________。', answer: '煜黄华叶衰' },
      { question: '窗含西岭千秋雪，_________。', answer: '门泊东吴万里船' },
      { question: '湖光秋月两相和，_________。', answer: '潭面无风镜未磨' },
      { question: '自古逢秋悲寂寥，_________。', answer: '我言秋日胜春朝' },
      { question: '银烛秋光冷画屏，_____________。', answer: '轻罗小扇扑流萤' },
      { question: '春花秋月何时了，_____________。', answer: '往事知多少' },
      { question: '欲说还休，_____________。', answer: '却道天凉好个秋' },
      // 更多题目...
    ];
    //var used = new Aray[50];
    for(let i=0;i<100;i++) {this.data.used[i]=0;}//标记位
    this.setData({ questions: sampleQuestions });
    this.setData({currentQuestion:Math.ceil(Math.random() * this.data.questions.length)})//随机显示第一道题目
  },

  startPractice: function() {
    // 开始练习，显示第一道题目
    this.setData({ showStartButton: false, showQuestion: true });
  },

  nextQuestion: function() {
    // 切换至下一道题目
  
    const currentQuestion = this.data.currentQuestion;
    let next=currentQuestion;//next为下一个题目序号
    this.data.used[currentQuestion]=1;//切换前先标记当前题目
    if (this.data.count < 9) {
      // 从题库中随机显示下一道题目
      while(this.data.used[next]==1){
        next=(next + Math.ceil(Math.random() * this.data.questions.length))%this.data.questions.length;
        console.log(next);
      }//循环至next为未标记的题号
      this.setData({
        currentQuestion: next,
        showQuestion: true,
        count: this.data.count+1
      });
      this.setData({ response: "" });//清空提示消息
    } else {
      // 已经是最后一道题目，可以在这里处理练习结束的逻辑
      this.setData({ response:"你已完成了全部练习！" });
    }
    
  },

  inputAnswer: function(event) {
    // 用户输入答案时更新题目数据中的用户答案字段
    console.log(event);
    const index = event.currentTarget.dataset.index;
    const answer = event.detail.value;
    this.data.questions[index].userAnswer = answer;
    console.log(this.data.questions[index]);
  },

  submitAnswers: function() {
    // 处理用户提交答案的逻辑
        // 获取当前题目的标准答案
        const correctAnswer = this.data.questions[this.data.currentQuestion].answer;
        // 获取用户提交的答案
        const userAnswer = this.data.questions[this.data.currentQuestion].userAnswer;
    
        // 判断用户答案是否与标准答案相同
        let response = "";

        if (userAnswer === correctAnswer) {
          response = "回答正确！";
        } 
        else {
          response = "回答错误，正确答案是：" + correctAnswer;
        }
        this.setData({ response: response });
        // 切换至下一道题目
        //this.nextQuestion();
  }
});